package deng.hibernate.examples.mapping;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main {
	public static void main(String[] args) throws Exception {
		insertAnimal();
		getAnimal();
	}

	private static EntityManager getEntityManager() {
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
		return emf.createEntityManager();
	}
	
	private static void getAnimal() {
		EntityManager em = getEntityManager();
		
		Animal animal = em.find(Animal.class, new Long(9223372036854775801L));
		System.out.println(animal);
		
		em.close();
	}	

	private static void insertAnimal() {
		EntityManager em = getEntityManager();
		EntityTransaction tx = em.getTransaction();
		tx.begin();
		
		Animal animal = new Animal();
		animal.setId(0);
		animal.setName("cat");
		System.out.println("Before: " + animal);
		em.persist(animal);
		System.out.println("After: " + animal);
		
		tx.commit();
		em.close();
	}
}
